package com.rsk.service.impl;


import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.rsk.dao.SysDeptRepository;
import com.rsk.dao.utils.BatchDataUitl;
import com.rsk.entity.SysDept;
import com.rsk.service.SysDeptService;
import com.rsk.utils.algorithm.SnowflakeIdWorker;




@Service(interfaceClass = SysDeptService.class)
public class SysDeptServiceImpl implements SysDeptService{
	
	@Autowired
	private SysDeptRepository sysDeptRrepository;
	
	@Autowired
	private BatchDataUitl batchDataUitl;

	//这个插入,得先看一下code  有没有数据,如果没有才插入成功,code 存在说明部门信息存在
	//部门保存
	@Transactional
	public int saveSysDept(String deptName, String deptCode, Long pId, Long operatorUser) {
		List<Map<String, Object>> list = sysDeptRrepository.selectSysDeptByDeptCode(deptCode);
		SysDept sysDept = new SysDept();
		 if(list.isEmpty()||list.size()<0){

			sysDept.setId(SnowflakeIdWorker.generateId(true));
			sysDept.setDeptName(deptName);
			sysDept.setDeptCode(deptCode);
			sysDept.setOperatorUser(operatorUser);
			sysDept.setOperatorTime(new Date());
			sysDeptRrepository.saveAndFlush(sysDept);
			return 1;
		}else {
			return -1;
		}
	}

	//部门修改
	@Transactional
	public int updateSysDept(Long id, String deptName, Long operatorUser) {
		return sysDeptRrepository.updateSysDept(id, deptName, operatorUser);
	}

	//部门删除
	@Transactional
	public int delSysDept(Long id, Long operatorUser) {
		return sysDeptRrepository.delSysDept(id, operatorUser);
	}

	//通过部门编号查询部门是否重复
	public List<Map<String, Object>> selectSysDeptByDeptCode(String deptCode) {
		// TODO Auto-generated method stub
		return sysDeptRrepository.selectSysDeptByDeptCode(deptCode);
	}
}
